package com.code.splider.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.code.splider.common.util.SAXUtil;
import com.code.splider.common.util.SXListUtils;
import com.code.splider.dao.DiaryDetailMapper;
import com.code.splider.dao.DiaryMapper;
import com.code.splider.model.Diary;
import com.code.splider.model.DiaryDetail;
import com.code.splider.service.DiaryService;
import com.code.splider.service.ScorePanluService;

@Service
public class DiaryServiceImpl implements DiaryService{

	private static String PREFIX = "http://www.dszuqiu.com";
	
	private static String URL = "http://www.dszuqiu.com/diary";

	@Resource
	private DiaryMapper diaryMapper;
	
	@Resource
	private DiaryDetailMapper diaryDetailMapper;
	
	@Resource
	private ScorePanluService scorePanluService;

	@Override
	public void save(Diary diary) {

		//主表数据实例化
		this.diaryMapper.insert(diary);
		
		//获取不同比赛日的数据的请求url
		String subUrl = PREFIX + diary.getHref();
		
		//获取不同比赛日的数据
		List<Map<String,String>> tabContents = SAXUtil.getTabContent(subUrl);
		
		List<String> zhiboIds = new ArrayList<>();
	
		//分别插入不同比三日的数据
		for(Map<String,String> temp : tabContents){
			DiaryDetail d = new DiaryDetail();
			d.setDiaryId(diary.getId());
			d.setLeague(temp.get("league"));
			d.setCompetitionTime(temp.get("beginTime"));
			d.setHomeTeam(temp.get("home_team"));
			d.setAwayTeam(temp.get("away_team"));
			d.setUpdateTime(new Date());
			String kou = temp.get("pan_kou");
			if(kou!=null){
			String[] pankou = kou.split("/");
			try{
				d.setConcedePoints(pankou[0]);
				d.setSizesBalls(pankou[1]);
				d.setCorner(pankou[2]);
			}catch (Exception e) {
				// TODO: handle exception
			}
			}
			if(temp.get("num3")!=null){
			String[] coner = temp.get("num3").split(":");
			if(!"-".equals(coner[0])){
				d.setHomeCorner(Integer.parseInt(coner[0].trim()));
				d.setAwayCorner(Integer.parseInt(coner[1].trim()));
			}
			}
			if(temp.get("num4")!=null){
			String[] score = temp.get("num4").split(":");
			if(!"-".equals(score[0])){
				d.setHomeScore(Integer.parseInt(score[0].trim()));
				d.setAwayScore(Integer.parseInt(score[1].trim()));
			}
			}
			zhiboIds.add(temp.get("zhiboId"));
			this.diaryDetailMapper.insert(d);
		}
		//scorePanluService.updateScorePanlu(zhiboIds);
	}

	@Override
	public void grapData() {
		
		//先删除上一次的数据
		this.diaryMapper.deleteAll();
		this.diaryDetailMapper.deleteAll();		
		
		List<Map<String,String>> contents = SAXUtil.getMenuContent(URL);
		if(!SXListUtils.isEmpty(contents)){
			
			for(Map<String,String> temp : contents){
				if("".equals(temp.get("href")) || "#".equals(temp.get("href"))){
					continue;
				}
				Diary d = new Diary();
				d.setName(temp.get("name"));
				d.setHref(temp.get("href"));
				d.setUpdateTime(new Date());
				try {
					this.save(d);
				} catch (Exception e) {
					// TODO: handle exception
				}
				
			}
		}	
		
	}
	
	public static void main(String[] args) {
		DiaryServiceImpl l = new DiaryServiceImpl();
		Diary diary= new Diary();
		//l.save(diary);
		diary.setHref("/diary/last");
	l.save(diary);
	}
	
}
